package com.castle.fortress.admin.system.rest;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.castle.fortress.admin.core.constants.GlobalConstants;
import com.castle.fortress.admin.system.entity.SysCountryEntity;
import com.castle.fortress.admin.system.dto.SysCountryDto;
import com.castle.fortress.admin.system.entity.SysUser;
import com.castle.fortress.admin.system.service.SysCountryService;
import com.castle.fortress.common.utils.ConvertUtil;
import com.castle.fortress.admin.utils.WebUtil;
import com.castle.fortress.common.entity.RespBody;
import com.castle.fortress.common.exception.BizException;
import com.castle.fortress.common.respcode.GlobalRespCode;
import com.castle.fortress.common.enums.OperationTypeEnum;
import com.castle.fortress.admin.core.annotation.CastleLog;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.logging.log4j.util.Strings;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 全球国家地区信息 api 控制器
 *
 * @author mjj
 * @since 2023-07-01
 */
@Api(tags="全球国家地区信息api管理控制器")
@RestController
@RequestMapping("/api/system/sysCountry")
public class ApiSysCountryController {
    @Autowired
    private SysCountryService sysCountryService;


    /**
     * 全球国家地区信息的分页展示
     * @param sysCountryDto 全球国家地区信息实体类
     * @param currentPage 当前页
     * @param size  每页记录数
     * @return
     */
    @CastleLog(operLocation="全球国家地区信息-api",operType = OperationTypeEnum.QUERY)
    @ApiOperation("全球国家地区信息分页展示")
    @GetMapping("/page")
    public RespBody<IPage<SysCountryDto>> pageSysCountry(SysCountryDto sysCountryDto, @RequestParam(required = false) Integer currentPage, @RequestParam(required = false)Integer size){
        Integer pageIndex= currentPage==null? GlobalConstants.DEFAULT_PAGE_INDEX:currentPage;
        Integer pageSize= size==null? GlobalConstants.DEFAULT_PAGE_SIZE:size;
        Page<SysCountryDto> page = new Page(pageIndex, pageSize);

        IPage<SysCountryDto> pages = sysCountryService.pageSysCountry(page, sysCountryDto);
        return RespBody.data(pages);
    }

    /**
     * 全球国家地区信息保存
     * @param sysCountryDto 全球国家地区信息实体类
     * @return
     */
    @CastleLog(operLocation="全球国家地区信息-api",operType = OperationTypeEnum.INSERT)
    @ApiOperation("全球国家地区信息保存")
    @PostMapping("/save")
    public RespBody<String> saveSysCountry(@RequestBody SysCountryDto sysCountryDto){
        if(sysCountryDto == null ){
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
            SysCountryEntity sysCountryEntity = ConvertUtil.transformObj(sysCountryDto,SysCountryEntity.class);
        if(sysCountryService.save(sysCountryEntity)){
            return RespBody.data("保存成功");
        }else{
            return RespBody.fail(GlobalRespCode.OPERATE_ERROR);
        }
    }

    /**
     * 全球国家地区信息编辑
     * @param sysCountryDto 全球国家地区信息实体类
     * @return
     */
    @CastleLog(operLocation="全球国家地区信息-api",operType = OperationTypeEnum.UPDATE)
    @ApiOperation("全球国家地区信息编辑")
    @PostMapping("/edit")
    public RespBody<String> updateSysCountry(@RequestBody SysCountryDto sysCountryDto){
        if(sysCountryDto == null || sysCountryDto.getId() == null || sysCountryDto.getId().equals(0L)){
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
            SysCountryEntity sysCountryEntity = ConvertUtil.transformObj(sysCountryDto,SysCountryEntity.class);
        if(sysCountryService.updateById(sysCountryEntity)){
            return RespBody.data("保存成功");
        }else{
            return RespBody.fail(GlobalRespCode.OPERATE_ERROR);
        }
    }

    /**
     * 全球国家地区信息删除
     * @param ids 全球国家地区信息id集合
     * @return
     */
    @CastleLog(operLocation="全球国家地区信息-api",operType = OperationTypeEnum.DELETE)
    @ApiOperation("全球国家地区信息删除")
    @PostMapping("/delete")
    public RespBody<String> deleteSysCountry(@RequestBody List<Long> ids){
        if(ids == null || ids.isEmpty()){
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
        if(sysCountryService.removeByIds(ids)) {
            return RespBody.data("保存成功");
        }else{
            return RespBody.fail(GlobalRespCode.OPERATE_ERROR);
        }
    }

    /**
     * 全球国家地区信息详情
     * @param id 全球国家地区信息id
     * @return
     */
    @CastleLog(operLocation="全球国家地区信息-api",operType = OperationTypeEnum.QUERY)
    @ApiOperation("全球国家地区信息详情")
    @GetMapping("/info")
    public RespBody<SysCountryDto> infoSysCountry(@RequestParam Long id){
        if(id == null){
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
            SysCountryEntity sysCountryEntity = sysCountryService.getById(id);
            SysCountryDto sysCountryDto = ConvertUtil.transformObj(sysCountryEntity,SysCountryDto.class);
        return RespBody.data(sysCountryDto);
    }


}
